package digital.store.springmvc.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


import org.springframework.stereotype.Repository;

import digital.store.springmvc.dao.OrderDao;
import digital.store.tutorial.springmvc.model.Orderk;

@Repository
public class OrderDaoImpl implements OrderDao{
	@PersistenceContext
	private EntityManager em;
	  
	public void saveOrUpdate(Orderk order) {
		if (order.getId() != null) {
	            em.merge(order);
			}
		else
			{
			 	em.persist(order);
			}
	    }
	    
	public void delete(Long id) {
	    Orderk order = findById(id);
	    em.remove(order);
	    }
	    
	@SuppressWarnings("unchecked")
	public List<Orderk> findAll() {
	    return em.createQuery("SELECT o FROM Orderk o").getResultList();
	    }
	    
    public Orderk findById(Long id) {
        return em.find(Orderk.class, id);
    }

}
